package com.yuqian.JavaSe.leetcode.easy;

import java.util.HashSet;
import java.util.Set;

/**
 * Given two arrays, write a function to compute their intersection.

Example:
Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].

Note:
Each element in the result must be unique.
The result can be in any order.
找出两个数组中公共的部分,结果中的元素必须唯一
 * @author yuqian
 *
 */
public class IntersectionofTwoArrays {
	 public static int[] intersection(int[] nums1, int[] nums2) {
	        if(nums1==null||nums1.length==0||nums2==null||nums2.length==0){
	        	return null;
	        }
	        int length1 = nums1.length;
	        int length2 = nums2.length;
	        //int max = Math.max(length1, length2);
	        //List<Integer> result = new ArrayList<Integer>(Math.min(length1, length2));
	        Set<Integer> result = new HashSet<Integer>();
	       //int[] result = new int[]{};
	       // Set<Integer> result = new HashSet<Integer>();
	        for(int i=0;i<length1;i++){
	        	for(int j=0;j<length2;j++){
	        		if(nums1[i]==nums2[j]){
        				result.add(nums1[i]);
	        		}
	        	}
	        }
	        int[] hh = new int[result.size()];
	        int ll=0;
	        for(Integer kk:result){
	        	hh[ll++] = kk;
	        }
	        return hh;
	    }
	 public static void main(String[] args) {
		 int[] nums1 = {1,1,2,3,4,5,2};
		 int[] nums2 = {2,7,2,3,8,2};
		 int[] result = intersection(nums1, nums2);
		 for(int m=0;m<result.length;m++){
				System.out.print(result[m]+",");
			}
		 System.out.println(null==null);
		  char s ='A';
		    int k= s;
		    System.out.println(k);
	}
}
